package com.alex.orderservice.controller;

import com.alex.orderservice.service.CuileiOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 崔磊订单控制器
 * 提供Web接口供用户下单
 */
@RestController
@RequestMapping("/cuilei/order")
public class CuileiOrderController {

    @Autowired
    private CuileiOrderService orderService;

    /**
     * 用户下单接口
     * 访问地址：http://localhost:8081/cuilei/order/create
     *
     * @param userId 用户ID
     * @param productName 商品名称
     * @param amount 金额
     * @return 订单结果
     */
    @PostMapping("/create")
    public Map<String, Object> createOrder(
            @RequestParam String userId,
            @RequestParam String productName,
            @RequestParam BigDecimal amount) {

        String orderId = orderService.createCuileiOrder(userId, productName, amount);

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "崔磊订单创建成功");
        result.put("orderId", orderId);
        result.put("userId", userId);
        result.put("productName", productName);
        result.put("amount", amount);

        return result;
    }

    /**
     * 简单的下单页面（用于测试）
     * 访问地址：http://localhost:8081/cuilei/order/test
     */
    @GetMapping("/test")
    public String testPage() {
        return """
            <html>
            <head>
                <title>崔磊订单测试页面</title>
                <meta charset="UTF-8">
            </head>
            <body>
                <h2>崔磊RabbitMQ订单测试</h2>
                <form action="/cuilei/order/create" method="post">
                    <p>用户ID: <input type="text" name="userId" value="cuilei_user_001"></p>
                    <p>商品名称: <input type="text" name="productName" value="崔磊的Java教程"></p>
                    <p>金额: <input type="number" name="amount" value="99.99" step="0.01"></p>
                    <p><input type="submit" value="立即下单"></p>
                </form>
            </body>
            </html>
            """;
    }






}
